const sqlExcute = require('../../database/query')
const path = require('path')

const handlerAddArticle = async (req, res) => {
  if (!req.file || req.file.fieldname !== 'cover_img') return res.cc('文章封面是必选参数！')
  // 获取前端传递的数据
  let article = req.body
  // 增加时间
  article.create_time = new Date()
  // 封面
  article.cover_img = path.join('./uploads', req.file.filename)
  // 作者id
  article.author_id = req.user.id
  // 执行sql
  const result = await sqlExcute(`insert into ev_articles set ?`, article);
  return result.affectedRows > 0 ? res.cc('新增文章成功', 1) : res.cc('新增文章失败')
}

module.exports = {
  handlerAddArticle,
}
